﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DocWaterMarker
{
    public static class EntitySiloExtension
    {
        public static void StoreCollection<T>(this Heroius.Files.EntitySilo silo, string key, ObservableCollection<T> collection)
        {
            for (int i = 0; i < collection.Count(); i++)
            {
                silo.Store($"{key}_{i}", collection.ElementAt(i));
            }
        }

        public static void FetchCollection<T>(this Heroius.Files.EntitySilo silo, string key, ObservableCollection<T> collection, Func<T> elementCreator)
        {
            collection.Clear();
            for (int i = 0;; i++)
            {
                var ckey = $"{key}_{i}";
                if (silo.ContainsInstanceKey(ckey))
                {
                    T no = elementCreator();
                    silo.Fetch(ckey, no);
                    collection.Add(no);
                }
                else
                {
                    break;
                }
            }
        }
    }
}
